﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using WMS.BasicData.Vendors;
using WMS.BasicData.Vendors.DTO;

namespace WMS.Web.Controllers.BasicData.Vendors
{
    public class VendorController : Controller
    {
        /// <summary>
        /// 供应商接口
        /// </summary>
        private readonly IVendorAppService _vendorAppService;


        /// <summary>
        /// 构造函数
        /// </summary>
        public VendorController(IVendorAppService vendorAppService)
        {
            _vendorAppService = vendorAppService;
        }


        /// <summary>
        /// 视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 录入
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 录入
        /// </summary>
        /// <returns></returns>
        public IActionResult Update(int Id)
        {
            ViewBag.Id = Id;
            return View();
        }


        /// <summary>
        /// 获取供应商（分页）
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">页容量</param>
        /// <param name="ChineseName">中文名字</param>
        /// <returns>返回对应数据</returns>
        /// Vendor/GetVendor
        [HttpGet]
        public async Task<JsonResult> GetVendor(int page, int limit, string? chineseName)
        {
            var pagedResult = await _vendorAppService.GetVendor(page, limit, chineseName);
            return Json(new { data = pagedResult, code = 0 });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="vendor">供应商信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> AddVendor(Vendor vendor)
        {
            var i = await _vendorAppService.AddVendor(vendor);
            return i;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id">主键Id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> DeleteVendor(int Id)
        {
            var i = await _vendorAppService.DeleteVendor(Id);
            return i;
        }

        /// <summary>
        /// 获取对应数据
        /// </summary>
        /// <param name="Id">主键Id</param>
        /// <returns>返回受影响行数</returns>
        public async Task<JsonResult> BackfillData(int Id)
        {
            var vendor = await _vendorAppService.BackfillData(Id);
            return Json(new { data = vendor });
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public async Task<int> UpdateVendor(UpdateVendorDto vendor)
        {
            var i = await _vendorAppService.UpdateVendor(vendor);
            return i;
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="array">数组</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> BulkDeletion(string array)
        {
            var i = await _vendorAppService.BulkDeletion(array);
            return 1;
        }

    }
}
